#include "JsonConfigurator.h"
#include <QMutex>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>

using namespace zl;

QMutex _GMutex;

int32_t CJsonConfigurator::Load(
	zl::AppConfig& config, 
    const QString & filepath)
{
	QMutexLocker locker(&_GMutex);
    QFile file(filepath);
	if (!file.open(QIODevice::ReadOnly))
	{
		return EResult_OpenFileFailed;
	}

	QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
	file.close();

	if (!doc.isObject())
	{
		return EResult_ParseJsonFailed;
	}

	// get config_
	QJsonObject obj = doc.object();

	QJsonObject cabobj = obj["cabinet"].toObject();
    QJsonObject	monitorobj = obj["monitor_info"].toObject();
    QJsonObject	snobj = obj["sn_info"].toObject();
    QJsonObject database = obj["database"].toObject();
	
    config.cabinet.name = cabobj["name"].toString();
    config.cabinet.title = cabobj["title"].toString();
    config.cabinet.cabinet_no = cabobj["cabinet_no"].toString();
    QString lang_value = cabobj["language"].toString();

    if (lang_value.compare("Cn") == 0)
        config.cabinet.language = ELanguageType_Cn;
    else if (lang_value.compare("En") == 0)
        config.cabinet.language = ELanguageType_En;

    config.monitor_config.port = monitorobj["serial_port_name"].toString();
    config.monitor_config.baud_rate = monitorobj["baudrate"].toString();

    int32_t ctl_power_value = monitorobj["monitor_control_power"].toInt();
    config.monitor_config.enable_monitor_control_power = (ctl_power_value > 0);

    config.sn_config.address = snobj["address"].toString();
    config.sn_config.sn_rule_len = snobj["sn_rule_len"].toInt();
    config.sn_config.sn_mem_bytes_len = snobj["mem_bytes"].toInt();

    /// 数据库配置选项
    config.database.type = database["type"].toInt();
    config.database.remote_port = database["port"].toInt();
    config.database.remote_addr = database["addr"].toString();
    config.database.user = database["user"].toString();
    config.database.pwd = database["pwd"].toString();

	return EResult_Success;
}

int32_t CJsonConfigurator::Save(
	const zl::AppConfig& config,
    const QString & filepath)
{
	QMutexLocker locker(&_GMutex);
    QFile file(filepath);
	if (!file.open(QIODevice::WriteOnly))
	{
		return EResult_OpenFileFailed;
	}

    QJsonObject cabobj, monitorobj, snobj, dbobj, obj;

    cabobj["name"] = config.cabinet.name;
    cabobj["title"] = config.cabinet.title;
    cabobj["cabinet_no"] = config.cabinet.cabinet_no;

    QString lang_value = "Cn";

    if (config.cabinet.language == ELanguageType_Cn)
        lang_value = "Cn";
    else if (config.cabinet.language == ELanguageType_En)
        lang_value = "En";

    cabobj["language"] = lang_value;

    monitorobj["serial_port_name"] = config.monitor_config.port;
    monitorobj["baudrate"] = config.monitor_config.baud_rate;
    monitorobj["monitor_control_power"] = (config.monitor_config.enable_monitor_control_power ? 1 : 0);

    snobj["address"] = config.sn_config.address;
    snobj["sn_rule_len"] = config.sn_config.sn_rule_len;
    snobj["mem_bytes"] = config.sn_config.sn_mem_bytes_len;

    /// 数据库配置信息
    dbobj["addr"] = config.database.remote_addr;
    dbobj["port"] = config.database.remote_port;
    dbobj["user"] = config.database.user;
    dbobj["pwd"] = config.database.pwd;
    dbobj["type"] = config.database.type;

	obj["cabinet"] = cabobj;	
    obj["monitor_info"] = monitorobj;
    obj["sn_info"] = snobj;
    obj["database"] = dbobj;

	if (file.write(QJsonDocument(obj).toJson()) < 0)
	{
		file.close();
		return EResult_WriteFileFailed;
	}
	file.close();

	return EResult_Success;
}
